﻿using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;

namespace Spiders
{
    class HttpWebRequestUtility
    {
        private static readonly string DefaultUserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";

        /// <summary>
        /// 构造基础HttpWebRequest -- GET 方法
        /// </summary>
        /// <param name="url"></param>
        /// <param name="userAgent"></param>
        /// <returns></returns>
        public static HttpWebRequest CreateGetHttpRequest(string url, string userAgent = "")
        {
            if (string.IsNullOrEmpty(url))
                throw new ArgumentNullException("url");

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Method = "GET";
            request.Timeout = 10000;
            if (!string.IsNullOrEmpty(userAgent))
                request.UserAgent = userAgent;
            else
                request.UserAgent = DefaultUserAgent;

            return request;
        }

        /// <summary>
        /// 构造基础HttpWebRequest -- POST 方法
        /// </summary>
        /// <param name="url"></param>
        /// <param name="userAgent"></param>
        /// <returns></returns>
        public static HttpWebRequest CreatePostHttpRequest(string url, string userAgent = "")
        {
            if (string.IsNullOrEmpty(url))
                throw new ArgumentNullException("url");

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Method = "POST";
            request.Timeout = 10000;
            if (!string.IsNullOrEmpty(userAgent))
                request.UserAgent = userAgent;
            else
                request.UserAgent = DefaultUserAgent;

            return request;
        }

        /// <summary>
        /// 读取响应流
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string ReadResponseStream(Stream stream)
        {
            MemoryStream memStream = new MemoryStream();
            MemoryStream newStream = new MemoryStream();
            stream.CopyTo(memStream);
            memStream.Position = 0;
            memStream.CopyTo(newStream);
            try
            {
                memStream.Position = 0;
                GZipStream gzipStream = new GZipStream(memStream, CompressionMode.Decompress);
                using (StreamReader reader = new StreamReader(gzipStream, Encoding.GetEncoding("gb2312")))
                {
                    return reader.ReadToEnd();
                }
            }
            catch
            {
                newStream.Position = 0;
                using (StreamReader newReader = new StreamReader(newStream, Encoding.GetEncoding("gb2312")))
                {
                    return newReader.ReadToEnd();
                }
            }
            finally
            {
                memStream.Close();
                newStream.Close();
            }
        }        
    }
}
